package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuItemSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuSLDTO;
import com.neusoft.neuiotms.dcam.workflow.model.Annex;
import com.neusoft.neuiotms.dcam.workflow.model.RukuAssetSL;
import com.neusoft.neuiotms.dcam.workflow.model.RukuItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.RukuSL;
import com.neusoft.neuiotms.dcam.workflow.repository.AnnexRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuAssetSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuSLRepository;

@Component
public class RukuSLConverter extends AbstractConverter<RukuSL, RukuSLDTO> {

    @Autowired
    private RukuSLRepository rukuSLRepository;
    @Autowired
    private RukuItemSLConverter rukuItemSLConverter;
    @Autowired
    private RukuItemSLRepository rukuItemSLRepository;
    @Autowired
    private AnnexRepository annexRepository;
    @Autowired
    private AnnexConverter annexConverter;
    @Autowired
    private RukuAssetSLConverter rukuAssetSLConverter;
    @Autowired
    private RukuAssetSLRepository rukuAssetSLRepository;

    @Override
    public RukuSL toModel(final RukuSLDTO dto) {
        final RukuSL model;
        if (dto.isNew()) {
            model = new RukuSL();
        } else {
            model = this.rukuSLRepository.getOne(dto.getId());
            final List<RukuItemSL> list = model.getArrivalCount().stream().collect(Collectors.toList());
            //删除到货验收数量信息
            list.forEach(k -> {
                model.getArrivalCount().remove(k);
                this.rukuItemSLRepository.delete(k);
            });

            final List<RukuAssetSL> assetList = model.getAssetList().stream().collect(Collectors.toList());
            assetList.forEach(k -> {
                model.getAssetList().remove(k);
                this.rukuAssetSLRepository.delete(k);
            });
        }

        //到货验收数量
        dto.getArrivalCount().stream().map(k -> {
            final RukuItemSL arrivalModel = this.rukuItemSLConverter.toModel(k);
            arrivalModel.setMain(model);
            return arrivalModel;
        }).filter(s -> null != s).forEach(model.getArrivalCount()::add);

        dto.getAssetList().stream().map(k -> {
            if ("3".equals(dto.getCategories())) {
                k.setLocation(dto.getLocation());
                k.setLocationName(dto.getLocationName());
            }
            final RukuAssetSL assetModel = this.rukuAssetSLConverter.toModel(k);
            assetModel.setRuku(model);
            return assetModel;
        }).filter(s -> null != s).forEach(model.getAssetList()::add);

        model.getAnnex().clear();
        dto.getAnnex().stream().map(k -> {
            Annex annex = this.annexConverter.toModel(k);
            annex = this.annexRepository.save(annex);
            return annex;
        }).forEach(model.getAnnex()::add);

        //合同
        model.setContractCode(dto.getContractCode());
        model.setContractName(dto.getContractName());
        model.setTitle(dto.getTitle());
        model.setRemark(dto.getRemark());
        model.setReceiveDate(dto.getReceiveDate());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setCreateUser(dto.getCreateUser());
        model.setCreateUserName(dto.getCreateUserName());
        model.setPurchaseDate(dto.getPurchaseDate());
        model.setCategories(dto.getCategories());
        model.setLocation(dto.getLocation());
        model.setLocationName(dto.getLocationName());

        return model;
    }

    @Override
    public RukuSLDTO toDTO(final RukuSL model, final boolean forListView) {
        final RukuSLDTO dto = new RukuSLDTO();

        dto.setContractCode(model.getContractCode());
        dto.setContractName(model.getContractName());
        dto.setCreateTime(model.getCreateTime());
        dto.setId(model.getId());
        dto.setReceiveDate(model.getReceiveDate());
        dto.setRemark(model.getRemark());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        dto.setCreateUser(model.getCreateUser());
        dto.setCreateUserName(model.getCreateUserName());
        dto.setPurchaseDate(model.getPurchaseDate());
        dto.setCategories(model.getCategories());
        dto.setLocation(model.getLocation());
        dto.setLocationName(model.getLocationName());
        dto.setArrivalCountSize(model.getArrivalCount().size());
        if (!forListView) {
            final List<RukuItemSL> acList = model.getArrivalCount();
            final List<RukuItemSLDTO> acDtoList = new ArrayList<>();
            for (final RukuItemSL ac : acList) {
                acDtoList.add(this.rukuItemSLConverter.toDTO(ac));
            }
            dto.setArrivalCount(acDtoList);

            if (null != model.getAssetList()) {
                model.getAssetList().stream().map(l -> this.rukuAssetSLConverter.toDTO(l))
                        .forEach(dto.getAssetList()::add);
            }
        }
        model.getAnnex().stream().map(k -> this.annexConverter.toDTO(k)).forEach(dto.getAnnex()::add);
        return dto;
    }
}
